//
//  ForgetPassWordVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/7/22.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "ForgetPassWordVC.h"

@interface ForgetPassWordVC ()

@end

@implementation ForgetPassWordVC
{
    UITextField *_phoneField;
    UITextField *_messCodeField;
    UITextField *_againNewPasswordTF;
    UITextField *_newPasswordTF;
    UIButton *_messCodeBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];//title字体颜色
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self initView];
}

- (void)initView{
    _phoneField = [[UITextField alloc]init];
    _phoneField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _phoneField.leftViewMode = UITextFieldViewModeAlways;
    _phoneField.layer.cornerRadius = 5;
    _phoneField.layer.borderWidth = 1;
    _phoneField.layer.borderColor = blackLineColore.CGColor;
    _phoneField.placeholder = @"请输入帐号(手机号码)";
    _phoneField.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:_phoneField];
    
    _messCodeField = [[UITextField alloc]init];
    _messCodeField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _messCodeField.leftViewMode = UITextFieldViewModeAlways;
    _messCodeField.layer.cornerRadius = 5;
    _messCodeField.layer.borderWidth = 1;
    _messCodeField.layer.borderColor = blackLineColore.CGColor;
    _messCodeField.placeholder = @"请输入验证码";
    [self.view addSubview:_messCodeField];
    
    _messCodeBtn = [[UIButton alloc]init];
    _messCodeBtn.backgroundColor = Theme_Color;
    _messCodeBtn.layer.cornerRadius = 5;
    _messCodeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [_messCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [_messCodeBtn addTarget:self action:@selector(messCodeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    _messCodeBtn.tintColor = [UIColor whiteColor];
    [self.view addSubview:_messCodeBtn];
    
    _newPasswordTF = [[UITextField alloc]init];
    _newPasswordTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _newPasswordTF.leftViewMode = UITextFieldViewModeAlways;
    _newPasswordTF.layer.cornerRadius = 5;
    _newPasswordTF.layer.borderWidth = 1;
    _newPasswordTF.layer.borderColor = blackLineColore.CGColor;
    _newPasswordTF.placeholder = @"请输入新密码";
    _newPasswordTF.secureTextEntry = YES;
    [self.view addSubview:_newPasswordTF];
    
    _againNewPasswordTF = [[UITextField alloc]init];
    _againNewPasswordTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    _againNewPasswordTF.leftViewMode = UITextFieldViewModeAlways;
    _againNewPasswordTF.layer.cornerRadius = 5;
    _againNewPasswordTF.layer.borderWidth = 1;
    _againNewPasswordTF.layer.borderColor = blackLineColore.CGColor;
    _againNewPasswordTF.placeholder = @"请再次输入新密码";
    _againNewPasswordTF.secureTextEntry = YES;
    [self.view addSubview:_againNewPasswordTF];
    
    UIButton *doneBtn = [[UIButton alloc]init];
    doneBtn.backgroundColor = Theme_Color;
    doneBtn.layer.cornerRadius = 5;
    [doneBtn setTitle:@"完成" forState:UIControlStateNormal];
    doneBtn.tintColor = [UIColor whiteColor];
    [doneBtn addTarget:self action:@selector(doneBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:doneBtn];
    
    //布局
    [_phoneField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(120),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [_messCodeField mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_phoneField.mas_bottom).offset(-1),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-110),
        make.height.mas_equalTo(40);
    }];
    
    [_messCodeBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_phoneField.mas_bottom).offset(-1),
        make.left.mas_equalTo(_messCodeField.mas_right).offset(-1),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [_newPasswordTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_messCodeField.mas_bottom).offset(20),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [_againNewPasswordTF mas_makeConstraints:^(MASConstraintMaker *make){
        make.top.mas_equalTo(_newPasswordTF.mas_bottom).offset(-1),
        make.left.mas_equalTo(10),
        make.right.mas_equalTo(-10),
        make.height.mas_equalTo(40);
    }];
    
    [doneBtn mas_makeConstraints:^(MASConstraintMaker *make){
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-100),
        make.left.mas_equalTo(52),
        make.right.mas_equalTo(-52),
        make.height.mas_equalTo(40);
    }];
}
//Btn点击事件
- (void)messCodeBtnAction{
    
    if (![_phoneField.text isValidateMobile]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
    }
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:_phoneField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            NSLog(@"获取验证码成功");
        }
        else
        {
            NSLog(@"错误信息：%@",error);
        }
    }];
    
    //更改button的颜色 并且设置button倒计时
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_messCodeBtn setBackgroundColor:Theme_Color];
                [_messCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                _messCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [_messCodeBtn setBackgroundColor:LightGray_Color];
                [_messCodeBtn setTitle:[NSString stringWithFormat:@"%@秒后重试",strTime] forState:UIControlStateNormal];
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                [UIView commitAnimations];
                _messCodeBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
- (void)doneBtnAction{
    //参数
    NSDictionary *parameters = @{
                                 @"phone":_phoneField.text,
                                 @"pass": _newPasswordTF.text,
                                 @"sysType": @"ios",
                                 @"code":_messCodeField.text,
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/user/changePass.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",dic);
        NSString *error = dic[@"code"];
        if (![dic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
        }else{
            //重置成功
            [SVProgressHUD showSuccessWithStatus:@"重置密码成功"];
            
            [self backAction];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"重置密码失败"];
    }];
}
-(void)backAction{
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
